import maya.cmds as mc


class LZ_core():
    def __init__(self):
        pass 
        
    def getCtrlList(self,assetRoot = '' ):
        """ 
        @brief Get a list of nodes in the controllers set
        @return : List
        """
        
        ctrlList = []
        
        #controllersSets = ['set_ctrl']
        controllersSets = mc.ls('*_ctrl')
        for eachSet in controllersSets :
        
            ## Check if the controllers set exists
            if mc.objExists(eachSet) is True :
                
                ## Get nodes under it
                getSet = mc.sets(eachSet, q=True)
                if not getSet is None :
                    
                    ## Append every nodes who are not in the exclusion list to ctrlList
                    for each in getSet :
                        if not mc.nodeType(each) in ['set'] :
                            ctrlList.append(each)
        
        
        return ctrlList
        
        
    def checkResult(self,**kw):
    
        d = { 'allowPublish' : None,
              'msg'          : '',
              'nodes'        : [],
              'warning'      : False,
            }
        
        d.update( kw )
        
        assert( d['allowPublish'] is True or d['allowPublish'] is False )
        assert( isinstance( d['msg'], basestring ) )
        assert( type(d['nodes']) is list )
        assert( d['warning'] is True or d['warning'] is False )
        
        return d
        
    def listStorableAttrs(self,node, shortName=True, noCompound=True):
        """ 
        @brief List every attributes of a node who can go trough the pipeline and whose value will be stored.
        @return : List
        
        @param node - String - Node to get the attributes of.
        @param shortName - Bool - If True, return attributes short name.
        @param noCompound - Bool - If True, exclude compound attributes.
        """
        
        def __getShortName(node, attr, shortName):
            result = attr
            if shortName:
                names = attr.split('.')
                snames = []
                for name in names:
                    if name.endswith(']'):
                        snames.append(name)
                    else:
                        snames.append(mc.attributeQuery(name, node=node, shortName=True))
                result = ".".join(snames)
        
            return result
        
        result = []
        nodeType = mc.nodeType(node)
        if not mc.listAnimatable(node):
            return result
        
        for s in mc.listAnimatable(node):
            if noCompound:
                if len(s.split('.')) > 2:
                    continue
        
            nodeName = s.split('.')[0]
            attr = ".".join(s.split('.')[1:])
        
            ## Remove attributes on the shapes (listAnimatable return them)
            if mc.nodeType(node) == mc.nodeType(nodeName):
                attr = __getShortName(node, attr, shortName)
                result.append(str(attr))
        
        attrs = mc.listAttr(node, cb=True)
        if attrs:
            for attr in attrs:
                attr = __getShortName(node, attr, shortName)
                result.append(str(attr))
        
        if mc.listAttr(node, userDefined=True):
            extra = [str(x) for x in mc.listAttr(node, userDefined=True) if mc.getAttr("%s.%s" % (node, x), type=True) == 'string' if not x.startswith("rnkInit_") if not mc.getAttr("%s.%s" % (node, x), lock=True)]
            if extra:
                result.extend(extra)
        
        return result
            
        
        